<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-03 00:33:05
 * @LastEditTime: 2023-10-03 14:17:19
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Models;

use App\Traits\MaterialTrait;
use App\Traits\StorageTrait;
use App\Traits\SoftDeletesTrait;
use App\Traits\CarTrait;
use App\Traits\CarryTrait;

class MarketRetailDetails extends BaseModel
    {
    use SoftDeletesTrait;
    use MaterialTrait;
    use StorageTrait;
    use CarryTrait;
    use CarTrait;
    public $timestamps = true;
    public $table = "sys_market_retail_details";
    protected $guarded = [];
    protected $casts = [];
    public function status()
        {
        return $this->belongsTo(Dic::class, "status_id", "key")->where(function ($q)
            {
            $q->where("code", 'status_with_finance');
            });
        }
    //运输派车原地址
    public function getSourceAddr()
        {
        return $this->creator->department?->name ?? "染厂";
        }
    //运输派车目的地址
    public function getTargetAddr()
        {
        return $this->customer->name;
        }
    public function tax()
        {
        return $this->belongsTo(Dic::class, "tax_id", "key")->where(function ($q)
            {
            $q->where("code", 'FinanceTax');
            });
        }
    public function main()
        {
        return $this->belongsTo(MarketRetail::class, "code", "code");
        }

    public function red()
        {
        return $this->hasMany(MarketComplaint::class, "red_id");
        }

    public function department()
        {
        return $this->belongsTo(Department::class);
        }
    public function _creating($model)
        {
        $model->color_material_code = $model->color_id . "_" . $model->material_id;
        }

    //设置客户余额
    public function _updating($model)
        {
        $model->color_material_code = $model->color_id . "_" . $model->material_id;

        if ($model->getOriginal("amount") != $model->amount) {
            $model->customer->updateBalance(0 - $model->getOriginal("amount"));
            $model->customer->updateBalance($model->amount);
            }
        //财务审核
        if ($model->getOriginal("status_id") != $model->status_id) {
            //审核
            if ($model->getOriginal("status_id") == 1 && $model->status_id == 2) {
                $model->customer->updateBalance(0 - $model->amount);
                // $model->customer->updateFinanceBalance($model->amount);
                }
            //反
            else if ($model->getOriginal("status_id") == 2 && $model->status_id == 1) {
                $model->customer->updateBalance($model->amount);
                // $model->customer->updateFinanceBalance(0 - $model->amount);
                }
            }
        }
    public function _created($model)
        {
        $model->customer->first()->updateBalance($model->se_amount);
        }
    //设置客户余额
    public function _deleted($model)
        {
        $model->customer->updateBalance(0 - $model->se_amount);
        }

    //恢复删除
    public function _restored($model)
        {
        $model->customer->updateBalance($model->se_amount);
        }

    public function customer()
        {
        return $this->belongsTo(Company::class, 'main_customer_id');
        }
    public function order()
        {
        return $this->belongsTo(CustomerStorageDetail::class, "batchno", "batchno");
        }
    }
